﻿//////////////////////////////////////////////
/// 实现 http 通信
//////////////////////////////////////////////
#pragma once
#include <QHttpServer>
#include <QHttpServerResponse>
#include <QProtobufSerializer>
#include <QTcpServer>
#include "ShowWidget.h"
#include "message_storage.qpb.h"
#include "message_transmit.qpb.h"
#include "user.qpb.h"

using namespace chat;
class HttpServer
{
private:
    const int HTTP_PORT = 9000;
public:
    static HttpServer* get_instance();
    bool               init();
public:
    /* 处理HTTP请求 */
    QHttpServerResponse handle_get_user_info(const QHttpServerRequest& request);
    QHttpServerResponse handle_get_friend_list(const QHttpServerRequest& request);
    QHttpServerResponse handle_get_chat_session_list(const QHttpServerRequest& request);
    QHttpServerResponse handle_get_friend_apply_list(const QHttpServerRequest& request);
    QHttpServerResponse handle_get_recent_msgs(const QHttpServerRequest& request);
    QHttpServerResponse handle_send_msg(const QHttpServerRequest& request);
    QHttpServerResponse handle_set_nickname(const QHttpServerRequest& request);
    QHttpServerResponse handle_set_desc(const QHttpServerRequest& request);
    QHttpServerResponse handle_get_phone_verify_code(const QHttpServerRequest& request);
    QHttpServerResponse handle_set_phone(const QHttpServerRequest& request);
    QHttpServerResponse handle_set_avatar(const QHttpServerRequest& request);
    QHttpServerResponse handle_delete_friend(const QHttpServerRequest& request);
    QHttpServerResponse handle_add_friend(const QHttpServerRequest& request);
    QHttpServerResponse handle_add_friend_process(const QHttpServerRequest& request);
    QHttpServerResponse handle_create_chat_session(const QHttpServerRequest& request);
    QHttpServerResponse handle_get_chat_session_member(const QHttpServerRequest& request);
    QHttpServerResponse handle_search_user(const QHttpServerRequest& request);
    QHttpServerResponse handle_search_history(const QHttpServerRequest& request);
    QHttpServerResponse handle_search_history_by_time(const QHttpServerRequest& request);
    QHttpServerResponse handle_login_by_username(const QHttpServerRequest& request);
    QHttpServerResponse handle_register_by_username(const QHttpServerRequest& request);
    QHttpServerResponse handle_login_by_phone(const QHttpServerRequest& request);
    QHttpServerResponse handle_register_by_phone(const QHttpServerRequest& request);
    QHttpServerResponse handle_get_file(const QHttpServerRequest& request);
    QHttpServerResponse handle_speech_recognition(const QHttpServerRequest& request);
private:
    /* 私有的工具方法 */
    HttpServer();
    HttpServer(const HttpServer&)            = delete;
    HttpServer& operator=(const HttpServer&) = delete;
    ~HttpServer();
    QByteArray                   read_file(const QString& file_name);
    qint64                       get_current_time_stamp();
    QHttpServerResponse          make_http_response(const QByteArray& data);
    std::unique_ptr<UserInfo>    make_user_info(const QByteArray& avatar, const QString& user_id,
                                                const QString& description, const QString& nick_name, const QString& phone);
    std::unique_ptr<MessageInfo> make_message_info(int index, const QString& chat_session_id, const QString& user_id, const QByteArray& avatar);
    std::unique_ptr<MessageInfo> make_image_info(int index, const QString& chat_session_id, const QString& user_id, const QByteArray& avatar);
    std::unique_ptr<MessageInfo> make_file_info(int index, const QString& chat_session_id, const QString& user_id, const QByteArray& avatar);
    std::unique_ptr<MessageInfo> make_speech_info(int index, const QString& chat_session_id, const QString& user_id, const QByteArray& avatar);
private:
    QHttpServer         http_server;
    QTcpServer*         tcp_server = nullptr;
    static HttpServer*  instance;
    QProtobufSerializer serializer;
    ShowWidget*         show_widget;
};